package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.dto.MenuDto2;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.dto.RoleDto2;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.MenuService;
import com.itheima.ydd.service.RoleMenuService;
import com.itheima.ydd.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/sys_role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;

    //角色分页查询
    @GetMapping("/list")
    public ResultData list(String name, int page, int limit){

        Page<Role> rolePage=new Page<>(page,limit);
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Role::getCreate_date);
        queryWrapper.like(Role::getName,name);
        roleService.page(rolePage,queryWrapper);

        /*Page<RoleDto2> roleDtoPage=new Page<>();
        BeanUtils.copyProperties(rolePage,roleDtoPage,"records");

        List<Role> records = rolePage.getRecords();
        List<RoleDto2> roleDtoList=records.stream().map((item->{
            RoleDto2 roleDto=new RoleDto2();
            BeanUtils.copyProperties(item,roleDto);
            LambdaQueryWrapper<RoleMenu> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(RoleMenu::getRole_id,item.getId());
            List<RoleMenu> list = roleMenuService.list(queryWrapper1);

            List<String> menuIds=list.stream().map((item2->{
                String menuId = item2.getMenu_id();
                return menuId;
            })).collect(Collectors.toList());

            roleDto.setRole_id(item.getId());
            roleDto.setMenu_id(menuIds);
            return roleDto;
        })).collect(Collectors.toList());

        roleDtoPage.setRecords(roleDtoList);*/


        PageResult pageResult = PageResult.init(rolePage.getTotal(),rolePage.getSize(),rolePage.getCurrent(),rolePage.getRecords());
        return ResultData.ok("",pageResult);
    }

    //角色详情
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        Role role = roleService.getById(id);

        LambdaQueryWrapper<RoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRole_id,id);
        List<RoleMenu> list = roleMenuService.list(queryWrapper);

        List<String> menuIds=list.stream().map((item->{
            String menuId = item.getMenu_id();
            return menuId;
        })).collect(Collectors.toList());

        //一级菜单
        LambdaQueryWrapper<Menu> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Menu::getPid,0);
        queryWrapper1.in(Menu::getId,menuIds);
        List<Menu> list1 = menuService.list(queryWrapper1);

        //遍历一级找二级
        List<MenuDto2> oneTwo=list1.stream().map((item->{
            MenuDto2 menuDto=new MenuDto2();
            BeanUtils.copyProperties(item,menuDto);

            //二级菜单
            LambdaQueryWrapper<Menu> queryWrapper2=new LambdaQueryWrapper<>();
            queryWrapper2.eq(Menu::getPid,item.getId());
            queryWrapper2.in(Menu::getId,menuIds);
            List<Menu> list2 = menuService.list(queryWrapper2);

            //遍历二级找三级
            List<MenuDto2> twoThree=list2.stream().map((item2->{
                MenuDto2 menuDto1=new MenuDto2();
                BeanUtils.copyProperties(item2,menuDto1);

                //三级菜单
                LambdaQueryWrapper<Menu> queryWrapper3=new LambdaQueryWrapper<>();
                queryWrapper3.eq(Menu::getPid,item2.getId());
                queryWrapper3.in(Menu::getId,menuIds);
                List<Menu> list3 = menuService.list(queryWrapper3);

                menuDto1.setChildren(list3);
                return menuDto1;
            })).collect(Collectors.toList());

            menuDto.setChildren(twoThree);
            return menuDto;

        })).collect(Collectors.toList());

        RoleDto2 roleDto=new RoleDto2();
        BeanUtils.copyProperties(role,roleDto);
        roleDto.setAuthList(oneTwo);

        return ResultData.ok("",roleDto);
    }

    //角色删除
    @GetMapping("/del")
    public ResultData del(String id){

        roleService.removeById(id);
        LambdaQueryWrapper<RoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRole_id,id);
        roleMenuService.remove(queryWrapper);
        return ResultData.ok("","删除成功");
    }

    //所有数据
    @GetMapping("/allData")
    public ResultData allData(){
        List<Role> list = roleService.list();
        return ResultData.ok("",list);
    }

    //添加
    @PostMapping("/add")
    public ResultData add(@RequestBody RoleDto2 roleDto){
        roleService.save(roleDto);

        List<String> menuids = roleDto.getMenuids();
        if (menuids==null){
            return ResultData.ok("","增加成功");
        }
        for (String menuid : menuids) {
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setMenu_id(menuid);
            roleMenu.setRole_id(roleDto.getId());
            roleMenuService.save(roleMenu);
        }
        return ResultData.ok("","增加成功");

    }

    // 修改
    @PostMapping("/update")
    public ResultData update(@RequestBody RoleDto2 roleDto){
        roleService.updateWithMenu(roleDto);
        return ResultData.ok("","修改成功");
    }


}
